global g_root "C:\Users\Hongye Guo\Dropbox (Personal)\Density\Code\Production\Submission"

* Set seed at the beginning---the result depends on when you set the seed for some reason
set seed 2025

use "${g_root}\Data\crsp_monthly_202106.dta", clear
gen ym = mofd(date)

generate x = runiform()
gen gp_var = mod(floor(1000000*x), 30)
gen sic4 = gp_var

* Apply the bare minimum filter
keep if inlist(shrcd, 10, 11, 12, 30, 31, 32)
drop if inlist(secstat, "Q", "W") 

replace prc = -prc if prc<0
gen mkval = prc * shrout

sort permno ym
tsset permno ym

* Lag mkval
gen mkval_l1m = L1.mkval
gen ret_f1m = F1.ret

drop if mi(mkval) | mi(ret_f1m) | mi(sic4)

sort ym 
by ym: egen temp = pctile(mkval) if primexch == "N", p(10)
by ym: egen p10_mkval_nyse = max(temp)
drop temp
drop if mkval<p10_mkval_nyse 

gen num = mkval * ret_f1m

sort ym sic4
by ym sic4: egen temp_num = total(num)
by ym sic4: egen ym_sic4_mkval = total(mkval)
gen ym_sic4_ret_f1m = temp_num/ym_sic4_mkval
drop temp_num 

sort ym 
by ym : egen temp_num = total(num)
by ym : egen ym_mkval = total(mkval)
gen ym_ret_f1m = temp_num/ym_mkval
drop temp_num 
drop num

sort ym sic4
by ym sic4: gen ym_sic4_count = _N
by ym sic4: keep if _n==1
	
gen ym_sic4_exret_f1m = ym_sic4_ret_f1m - ym_ret_f1m

tsset sic4 ym 
forvalues i = 1/12{
	local im1 = `i' - 1
	gen ym_sic4_exret_l`im1'm = L`i'.ym_sic4_exret_f1m
}

sort ym sic4 
by ym: egen temp_den = total(ym_sic4_mkval)
gen wgt = ym_sic4_mkval/temp_den
drop temp_den

gen month = month(dofm(ym + 1))
gen am_flag = mod(month, 3) == 1

forvalues i = 1/4{
	local j = (`i'-1)*3 
	local jp1 = `j' + 1
	local jp2 = `j' + 2
	foreach l_var in ym_sic4_exret {
		gen `l_var'_prevq_l`i'q = `l_var'_l`j'm if mod(month,3)==2
		replace `l_var'_prevq_l`i'q = `l_var'_l`jp1'm if mod(month,3)==0
		replace `l_var'_prevq_l`i'q = `l_var'_l`jp2'm if mod(month,3)==1
		gen `l_var'_prevq_l`i'q_xam = `l_var'_prevq_l`i'q * am_flag
	}
}

gen ym_sic4_exret_prevq_t4q = ym_sic4_exret_prevq_l1q + ym_sic4_exret_prevq_l2q + ym_sic4_exret_prevq_l3q + ym_sic4_exret_prevq_l4q
gen ym_sic4_exret_prevq_t4q_xam = ym_sic4_exret_prevq_t4q * am_flag

* Table 10: column 10
local col_count = 0
clear matrix

reg ym_sic4_exret_f1m ym_sic4_exret_prevq_t4q am_flag ym_sic4_exret_prevq_t4q_xam [weight = wgt] if ym_sic4_count>19, cluster(ym)		
local col_count = `col_count' + 1
estimates store sep_`col_count'	

local l_date : display %tdCYND date(c(current_date), "DMY")
local l_time = substr(c(current_time), 1, 2) + substr(c(current_time), 4, 2)
esttab sep_* using "$g_root/Output/Split_random_`l_date'`l_time'.csv", star(* 0.1  ** 0.05 *** 0.01) b(3) t(2) r2 br noomit replace 
